package org.anddev.andengine.extension.svg.node;
/**
 * @author Eric Depta
 * @since 01:14:00 - 28.02.2011
 */
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.extension.svg.SVGDoc;
import org.anddev.andengine.extension.svg.util.physic.ExtendedPhysicsFactory;
import org.anddev.andengine.extension.svg.util.render.Render;
import org.anddev.andengine.extension.svg.util.transform.TransformDoc;
import org.anddev.andengine.util.SAXUtils;
import org.xml.sax.Attributes;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class RectNode extends DrawablePhysicNode{
	
	public RectNode(final SVGDoc pSVGDoc){
		super(pSVGDoc);
	}
	
	public void create(final Attributes pAttributes, final TransformDoc pTransform){
		super.create(pAttributes, pTransform);
	}
	
	@Override
	protected Shape createShape(final Attributes pAttributes){
		final String source = SAXUtils.getAttribute(pAttributes, TAG_ATTRIBUTE_SPRITESRC, "");
		Shape shape = null;
		if(source != "" && !super.mSVGDoc.getDebug() && super.mSVGDoc.mTextureManager != null){
			//-- Sprite
			shape = super.createSprite(
				pAttributes, 
				source,
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_X, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_Y, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_WIDTH, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_HEIGHT, 0),
				0
			);
			super.setHasSprite(true);
		}else if(super.isHasPhysic() || super.isHasDrawables()){
			//-- Boundings
			shape = new Rectangle(
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_X, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_Y, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_WIDTH, 0), 
				SAXUtils.getFloatAttribute(pAttributes, TAG_ATTRIBUTE_HEIGHT, 0)
			);
		}

		//-- Transform
		super.mTransformation.transformShape(shape);
		
		super.drawPrimitives(pAttributes, shape);
		
		return shape;
	}
	
	@Override
	protected void drawPrimitivesFill(final Shape pShape, final float[] pColor, final float pAlpha){
		pShape.setColor(
			pColor[0], 
			pColor[1], 
			pColor[2], 
			pAlpha
		);
	}
	
	@Override
	protected void drawPrimitivesLine(final Shape pShape, final float[] pColor, final float pAlpha, final float pWidth, final boolean pAntiAlias){
		final Vector2[] verts = new Vector2[4];
		verts[0] = new Vector2(0,0);
		verts[1] = new Vector2(pShape.getWidth(),0);
		verts[2] = new Vector2(pShape.getWidth(),pShape.getHeight());
		verts[3] = new Vector2(0,pShape.getHeight());
		Render.lineRenderRel(
			verts, 
			pShape, 
			pColor, 
			pAlpha, 
			pWidth,
			pAntiAlias,
			true
		);
	}
	
	
	@Override
	protected Body createPhysics(final Shape pShape, final Attributes pAttributes){
		if(pShape == null) return null;
		Body body = null;
		if(super.isHasPhysic() && super.mSVGDoc.mPhysicsWorld != null){
			body = ExtendedPhysicsFactory.createBoxBody(
				super.mSVGDoc.mPhysicsWorld, 
				pShape,
				BodyType.values()[SAXUtils.getIntAttribute(pAttributes, TAG_ATTRIBUTE_BODYTYPE, 0)], 
				super.createFixtureDef(pAttributes)
			);
			super.setBodyOptions(pAttributes, body);
			super.registerPhysicsConnector(pAttributes, pShape, body);
		}else if(super.isHasDrawables()){
			//-- Velocitys to shape
			super.createPhysicHandler(pAttributes, pShape);
		}
		return body;
	}
}
